﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace DBSourceToolsLib.CodeParser
{
    public class ParseVerb : ParseObject
    {
        public const int SELECT = 1;
        public const int FROM = 2;
        public const int WHERE = 3;
        public const int NAME = 4;
        public const int FILENAME = 5;
        public const int SIZE = 6;
        public const int MAXSIZE = 7;
        public const int FILEGROWTH = 8;

        private static Hashtable m_KeywordHash;

        public ParseVerb(string strValue, int iLineIdx, int iWordIdx)
            : base(ParseObject.UNKNOWN, ParseObject.VERB, false, iLineIdx, iWordIdx)
        {
            m_Value = computeValue(strValue.Trim());
            if (m_Value > 0)
                IsResolved = true;
            m_Data = strValue.Trim();
        }

        public static int computeValue(string strValue)
        {
            if (m_KeywordHash.ContainsKey(strValue.ToLower()))
                return (int)m_KeywordHash[strValue.ToLower()];
            else
                return -1;
        }



        static ParseVerb()
        {
            m_KeywordHash = new Hashtable();
            m_KeywordHash.Add("select", SELECT);
            m_KeywordHash.Add("from", FROM);
            m_KeywordHash.Add("where", WHERE);
            m_KeywordHash.Add("name", NAME);
            m_KeywordHash.Add("filename", FILENAME);
            m_KeywordHash.Add("size", SIZE);
            m_KeywordHash.Add("maxsize", MAXSIZE);
            m_KeywordHash.Add("filegrowth", FILEGROWTH);
        }

        public ParseVerb(string strCurrentValue, object oNextChar, int iLineIdx, int iWordIdx)
            : base(ParseObject.UNKNOWN, ParseObject.VERB, false, iLineIdx, iWordIdx)
        {
            m_Value = computeValue(strCurrentValue.Trim(), oNextChar);
            if (m_Value > 0)
                IsResolved = true;
            m_Data = strCurrentValue.Trim();
        }

        public static int computeValue(string strCurrentValue, object oNextChar)
        {
            if (oNextChar != null)
            {
                char cNextChar = (char)oNextChar;
                if (char.IsLetterOrDigit(cNextChar))
                    return -1;
                else
                    return computeValue(strCurrentValue);
            }
            else
            {
                return computeValue(strCurrentValue);
            }
        }
        public bool IsLegal
        {
            get { return m_Value > 0; }
        }

        
    }
}
